package com.lmx.persistence.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lmx.persistence.service.common.BaseCrudServiceImpl;
import com.lmx.repoDomain.tmodel.EvaluateExamerAnswer;

/**
 *
 * @author ytche
 * @date 2017年8月22日
 *
 */
@Service
public class EvaluateExamerAnswerServiceImpl extends BaseCrudServiceImpl<EvaluateExamerAnswer>
		implements EvaluateExamerAnswerService {
	@Autowired
	EvaluateResultService resultService;

	// @Override
	// public EvaluateExamerAnswer create(EvaluateExamerAnswer entity) {
	// Integer answerScore = new Integer(entity.getAnswer().getScore());
	// EvaluateResult result =
	// this.resultService.findOne(entity.getResult().getId());
	// Integer resultScroe = StringUtils.isNotBlank(result.getScore()) ? new
	// Integer(
	// result.getScore())
	// + answerScore : answerScore;
	// result.setScore(resultScroe.toString());
	// result.setAnswers(null);
	// resultService.save(result);
	// return super.save(entity);
	// }
	//
	// @Override
	// public EvaluateExamerAnswer update(EvaluateExamerAnswer entity) {
	// Integer answerScore =
	// CommonConstants.DEL_STATUS_YES_1.equals(entity.getDelStatus()) ? 0 - new
	// Integer(
	// entity.getAnswer().getScore()) : new
	// Integer(entity.getAnswer().getScore());
	// EvaluateResult result =
	// this.resultService.findOne(entity.getResult().getId());
	// Integer resultScroe = StringUtils.isNotBlank(result.getScore()) ? new
	// Integer(
	// result.getScore())
	// + answerScore : answerScore;
	// result.setScore(resultScroe.toString());
	// result.setAnswers(null);
	// resultService.save(result);
	// return super.save(entity);
	// }

	@Override
	@Transactional
	public EvaluateExamerAnswer setOtherAnswers(EvaluateExamerAnswer answer) {
		if (answer.getQuestion().getId() != null && answer.getResult().getId() != null) {
			StringBuilder query = new StringBuilder(
					"update  EvaluateExamerAnswer rp set rp.selectFlag = ?1  where rp.delStatus!=1 and rp.result.id=?4 and rp.question.id=?3 and rp.id!=?2");
			this.em.createQuery(query.toString()).setParameter(4, answer.getResult().getId()).setParameter(3, answer.getQuestion().getId()).setParameter(1, 0)
					.setParameter(2, answer.getId()).executeUpdate();
		}
		return answer;
	}
}



//List<EvaluateExamerAnswer> answers = this.em
//.createQuery(" select q from EvaluateExamerAnswer q where q.delStatus!=1 and q.question.id=?1 ")
//.setParameter(1, answer.getQuestion().getId()).getResultList();
//if (answers != null && answers.size() > 0) {
//StringBuilder query = new StringBuilder(
//	"update  EvaluateExamerAnswer rp set rp.selectFlag = ?1  where rp.delStatus!=1 and  rp in (:answers) and rp.id!=?2");
//this.em.createQuery(query.toString()).setParameter("answers", answers).setParameter(1, 0).setParameter(2, answer.getId()).executeUpdate();
//}
